package cc.jorange.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cc.jorange.service.PosterService;
import cc.jorange.service.WorkService;

import com.google.common.collect.Lists;

/**
 * 基础
 * 
 * @author Aaron
 *
 */
@Controller
public class HomeController {

	@Autowired
	private WorkService workService;
	@Autowired
	private PosterService posterService;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index(Model model) throws Exception {
		List<Long> ids = Lists.newArrayList();
        ids.add(83L);
        ids.add(84L);
        ids.add(75L);
        ids.add(74L);
		ids.add(4L);
		ids.add(6L);
		ids.add(1L);
		ids.add(14L);
		model.addAttribute("works", workService.findInId(ids));
		model.addAttribute("poster", posterService.findAll());
		return "index";
	}

	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String postSingle(@PathVariable Long id, Model model) throws Exception {
		List<Long> ids = Lists.newArrayList();
		ids.add(13L);
		ids.add(4L);
		ids.add(14L);
		List<Map<String, Object>> works = workService.findInId(ids);
		List<Map<String, Object>> work = Lists.newArrayList();
		for (Long i : ids) {
			for (Map<String, Object> w : works) {
				if (i == (Long) w.get("id")) {
					work.add(w);
				}
			}
		}
		model.addAttribute("recommend", work);
		model.addAttribute("work", workService.findById(id));
		return "detail";
	}

	@RequestMapping(value = "/work/{date}", method = RequestMethod.GET)
	public String work(@PathVariable String date, Model model) throws Exception {
		model.addAttribute("works", workService.findAll(date));
		return "work";
	}

}
